package com.wjyup.coolq.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;

import com.wjyup.coolq.entity.WeatherInfo;

public class WebUtil {
	private static Logger log = Logger.getLogger(WebUtil.class); // 日志
	
	//天气缓存
	public static Map<String, WeatherInfo> weatherInfo = new HashMap<>();
	
	
	
	public static String sendInfo( String data) {

        CloseableHttpClient client = HttpClients.createDefault();

       HttpPost post = new HttpPost("http://"+ConfigCache.WSHost+":"+ConfigCache.WSPort);

        StringEntity myEntity = new StringEntity(data,

            ContentType.APPLICATION_JSON);// 构造请求数据

       post.setEntity(myEntity);// 设置请求体

       String responseContent = null; // 响应内容

        CloseableHttpResponse response = null;

        try {

           response = client.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {

                HttpEntity entity = response.getEntity();

               responseContent = EntityUtils.toString(entity, "UTF-8");

          }

       } catch (ClientProtocolException e) {

         e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

               if (response != null)

                   response.close();



            } catch (IOException e) {

               e.printStackTrace();

            } finally {

              try {

                   if (client != null)

                      client.close();

               } catch (IOException e) {

                  e.printStackTrace();

               }

           }

        }

        return responseContent;

}

	
	/**
	 * 根据URL和请求类型获取请求结果，用于跨域请求
	 * @param url
	 * @param urlType POST OR GET
	 * @return
	 */
	public static String getUrlResult(String url,String urlType){
		Method method = null;
		if(urlType!= null && !"".equals(urlType) && urlType.equalsIgnoreCase("post")){
			method = Method.POST;
		}else{
			method = Method.GET;
		}
		Response resp = null;
		try {
			resp = Jsoup.connect(url).userAgent("Chrome").timeout(5000).ignoreContentType(true).method(method).execute();
		} catch (IOException e) {
			log.error(WebUtil.class,e);
		}
		if(resp == null){
			return "error";
		}else{
			return resp.body();
		}
	}
	
	/**
	 * 根据URL和请求类型获取请求结果，用于跨域请求
	 * @param url
	 * @param method Method.POST Method.GET
	 * @param data 参数
	 * @param cookie cookies
	 * @return
	 */
	public static String fetch(String url, Method method, Map<String, String> data, Map<String, String> cookie){
		Response resp = null;
		try {
			resp = Jsoup.connect(url).userAgent("Chrome").timeout(5000)
					.data(data)
					.cookies(cookie)
					.ignoreContentType(true).method(method).execute();
		} catch (IOException e) {
			log.error(WebUtil.class,e);
		}
		return resp.body();
	}
}
